/**
 * 武器定义表
 */
class WeaponTpl {
    private static _instance:WeaponTpl;
    private weaponData:Object;

    constructor()
    {
        this.weaponData = JsonUtil.getData('weaponDefine_json');  
    }
    
    public static instance():WeaponTpl
    {
        if(!this._instance)
        {
            this._instance = new WeaponTpl();
        }
        return this._instance;
    }

    public getRandomWeaponId(type)
    {
        let data = this.getDataByType(type);
        let length = data.length;
        let index = Math.floor(Math.random() * length + 1);
        let weapon = data[index - 1];
        let weaponId = 1;

        if(weapon)
        {
            weaponId = weapon.weaponData.id;
        }
        return weaponId;
    }

    public getWeaponDataByWeaponId(weaponId)
    {
        if(!this.weaponData[weaponId])
        {
            LogUtil.error('error weaponId, dont exist in the WeaponTpl:'+weaponId);
            return null;
        }
        return this.weaponData[weaponId];
    }

    public getDataByWeaponId(weaponId)
    {
        if(!this.weaponData[weaponId])
        {
            LogUtil.error('error weaponId, dont exist in the WeaponTpl:'+weaponId);
            return null;
        }
        let data = {
            weaponData:this.weaponData[weaponId],
            itemData:null
        }
        let itemDefine = DataJson.item_define;
        let itemId = parseInt(weaponId) + 4;
        if(itemDefine[itemId].relatedId == weaponId)
        {
            data.itemData = itemDefine[itemId];
        }
        else
        {
            for(let k in itemDefine)
            {
                let v = itemDefine[k];
                if(v.relatedId == weaponId)
                {
                    data.itemData = v;
                }
                break;
            }
            if(!data.itemData)
            {
                LogUtil.error('error weaponId, dont exist in the itemData:'+weaponId);
                return null;
            }
        }
        return data;
    }

    public getGiftWeapon()
    {
        let data = [];
        let curHadWeapon = RoleCache.instance().curHadWeapon;
        for(let k in this.weaponData)
        {
            let v = this.weaponData[k];
            if(v.shop == 2 && curHadWeapon.indexOf(v.id+'') == -1)
            {
                data.push(v);
            }
        }
        let index = Math.floor(Math.random() * data.length);
        return  this.getDataByWeaponId(data[index].id);
    }

    public getDataByType(type)
    {
        let data = [];
        if(type == weaponType.self)
        {
            let curHadWeapon = RoleCache.instance().curHadWeapon;
            for(let v of curHadWeapon)
            {
                let w = this.getDataByWeaponId(v);
                data.push(w);
            }
        }
        else
        {
            let curHadWeapon = RoleCache.instance().curHadWeapon;
            for(let k in this.weaponData)
            {
                let v = this.weaponData[k];
                if(v.type == type && curHadWeapon.indexOf(v.id+'') == -1)
                {
                    let w = this.getDataByWeaponId(v.id);
                    data.push(w);
                }
            }
        }
        return data;
    }
}
